import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by FCMMY on 2017/4/25.
 */
public class Files {
    private static String filePath ;
    private static String outPath ;
    private static String ext;

    private static String fileNamesStr = "";

    /**
     * 带参数的构造方法
     * @param filePath 遍历目录
     * @param outPath  输出文件
     * @param ext      过滤拓展名
     */
    public Files(String filePath, String outPath,String ext){
        this.filePath = filePath;
        this.outPath = outPath;
        this.ext = ext;
        getName(this.filePath);
        //System.out.println(fileNamesStr);
        writeToTxt(fileNamesStr);
    }
    /**
     * 获取文件列表
     * @param path
     */
    public static void getName(String path) {
        File file = new File(path);
        if (file.isDirectory()) {
            File[] dirFile = file.listFiles();
            for (File f : dirFile) {
                if (f.isDirectory()){
                    getName(f.getAbsolutePath());
                } else {
                    if (f.getName().endsWith(ext)){
                        String fName = f.getAbsolutePath().toString();
                        fileNamesStr += fName + "\r\n";
                    }
                }
            }
        }
    }

    /**
     * 结果输出到文件
     * @param content
     */
    public static void writeToTxt(String content){
        File outFile = new File(outPath);
        FileOutputStream o=null;
        try {
            o = new FileOutputStream(outFile);
            o.write(fileNamesStr.getBytes("UTF-8"));
            o.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
